package com.ulrica.morning.tools;

import lombok.SneakyThrows;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 疫情情况
 *
 * @author LongKui
 * @since 2022/08/16
 */
public class EpidemicSituation {
    private static final String URI = "http://wjw.xinjiang.gov.cn/%s";

    @SneakyThrows
    public static void main(String[] args) {

        String path = "hfpc/tzgg/zfxxgk_gknrz.shtml";
        String info = "新疆维吾尔自治区新型冠状病毒肺炎疫情最新情况";
        Connection connect = Jsoup.connect(String.format(URI, path));
        Document doc = connect.get();
        Elements elements = doc.getElementsByTag("dd");
        for (Element element : elements) {
            Elements select = element.select("a");
            if (!select.text().contains(info)) {
                continue;
            }
            String href = select.attr("href").trim();
//            System.out.printf("%s -> %s%n", select.text(), href);
            epidemicSituationInfo(select.text(), href);
            Thread.sleep(10000);
        }
    }


    @SneakyThrows
    public static void epidemicSituationInfo(String name, String href) {
        Connection connect = Jsoup.connect(String.format(URI, href));
        Document document = connect.get();
        Elements elements = document.getElementsByClass("gknbxq_detail");
        System.out.println(name);
        System.out.println("------------------------");
        for (Element element : elements) {
            Elements pElements = element.select("p");
            getGknbxqDetail(pElements);
        }
        System.out.println("===========================\n===========================");
    }

    public static void getGknbxqDetail(Elements elements) {
        for (Element element : elements) {
            String text = element.text();
            if(!text.contains("截至")){continue;}
            StringBuilder data = new StringBuilder();
            String[] split = text.split("，");
            for (String s : split) {
                if(s.contains("；")){
                    for (String head : s.split("；")) {
                        if (head.contains("新增确诊病例")
                                || head.contains("新增无症状感染者")
                                || head.contains("现有确诊病例")
                                || head.contains("现有无症状感染者")) {
                            data.append(head);
                            data.append(" ");
                        }
                    }
                    continue;
                }
                if (s.contains("新增确诊病例")
                        || s.contains("新增无症状感染者")
                        || s.contains("现有确诊病例")
                        || s.contains("现有无症状感染者")) {
                    data.append(s);
                    data.append(" ");
                }

                if (s.contains("哈密")) {
                    data.append(s);
                    data.append(" ");
                }
            }

            System.out.println(data);
        }

    }

}
